package com.hh.controller;

import com.hh.common.Const;
import com.hh.domain.ResponseResult;
import com.hh.entity.vo.LogininfoVO;
import com.hh.entity.vo.StudentLoginVO;
import com.hh.entity.vo.TeacherLoginVO;
import com.hh.enums.AppHttpCodeEnum;
import com.hh.service.LoginService;
import com.hh.utils.ObjectUtils;
import com.hh.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/*
 * @Author huxy
 * @since 2022年10月16日
 * */
@RestController
public class LoginController {
    @Autowired
    LoginService loginService;
    @Autowired
    RedisCache redisCache;
    /*
     * 登录
     * @commen 用户登录，无鉴权，故根据roleId判断登录角色，前端给予相应模块操作权限
     * @Params 登录信息 （LogininfoVO 登录Dto）
     * @return 登录结果
     * */
    @RequestMapping("/login")
    public ResponseResult login(@RequestBody LogininfoVO login){
        StudentLoginVO studentdto = new StudentLoginVO();
        TeacherLoginVO teacherdto =new TeacherLoginVO();
        String fcode = login.getFcode();
        Object obcode = redisCache.getCacheObject(Const.CAPTCHA_KEY);
        String code = String.valueOf(obcode);
        if (!fcode.equals(code)){
            return ResponseResult.errorResult(AppHttpCodeEnum.CODE_ERROR);
        }
        switch (login.getRoleId()){
            case 1 :
                studentdto = loginService.studentLogin(login);
                //这个地方很奇怪，studentdto永远会有个fcode带在里面，不管空不空都会判断为fcode变量
                if (ObjectUtils.isEmpty(studentdto.getFcode())){
                    return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ERROR);
                }
                return new ResponseResult(AppHttpCodeEnum.SUCCESS.getCode(),studentdto);
            case 2 :
                teacherdto = loginService.teacherLogin(login);
                if (ObjectUtils.isEmpty(teacherdto.getFcode())){
                    return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ERROR);
                }
                return new ResponseResult(AppHttpCodeEnum.SUCCESS.getCode(),teacherdto);
            case 3 :
                teacherdto = loginService.teacherLogin(login);
                if (ObjectUtils.isEmpty(teacherdto.getFcode())){
                    return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ERROR);
                }
                return new ResponseResult(AppHttpCodeEnum.SUCCESS.getCode(),teacherdto);
        }
    return ResponseResult.okResult();
    }
}
